package ZengJiHong2.Test5;

/**
 * @ClassName test5
 * @Author 16799
 * @Date 2025/8/20 14:39
 * @Version 1.0
 * @Description TODO
 *
 *  时间：2025年8月20日16:53:44
 **/
public class test5 {
    public static void main(String[] args) {
        int[] nums = {5, 7, 7, 7, 7, 8, 8, 9};
        int leftindex = getLeftIndex(nums,7);
        int rightindex = getRightIndex(nums,7);

        System.out.println("["+leftindex+","+rightindex+"]");
    }

    public static int getLeftIndex(int[] nums,int target){
        int start = 0;
        int end = nums.length-1;
        //假设数据不存在
        int rs =-1;
        while (start<=end){
            int middle = (start+end)/2;
            if (target<nums[middle]){
                end = middle-1;
            }else if (target>nums[middle]){
                start = middle+1;
            }else {
                //在middle索引位置处找到了目标元素
                rs = middle;
                end = middle-1;
            }
        }
        return rs;
    }

    public static int getRightIndex(int[] nums,int target){
        int start = 0;
        int end = nums.length-1;
        int rs =-1;
        while (start<=end){
            int middle = (start+end)/2;
            if (target>nums[middle]){
                start = middle+1;
            }else if (target<nums[middle]){
                end = middle-1;
            }else {
                rs = middle;
                start = middle+1;
            }
        }
        return rs;
    }
}
